from PyQt5.QtWidgets import QListWidget, QLineEdit, QCheckBox, QListWidgetItem


class ComboCheckBox():
    def __init__(self, coBox, defaultText="",callable=None):
        self.coBox = coBox
        self.defaultText = defaultText
        self.callable = callable
        self.items = []
        self.isAll = False
        self.row_num = 0
        self.selectedrow_num = 0
        self.qCheckBox = []
        self.selectData = []
        self.qListWidget = QListWidget()
        self.qLineEdit = QLineEdit()
        self.qLineEdit.setReadOnly(True)
        coBox.setLineEdit(self.qLineEdit)

    def addQCheckBox(self, i):
        self.qCheckBox.append(QCheckBox())
        qItem = QListWidgetItem(self.qListWidget)
        self.qCheckBox[i].setText(self.items[i])
        self.qListWidget.setItemWidget(qItem, self.qCheckBox[i])

    def getCheckItems(self):
        checkedItems = []
        for i in range(1, self.row_num):
            if self.qCheckBox[i].isChecked() == True:
                checkedItems.append(self.qCheckBox[i].text())
        self.selectedrow_num = len(checkedItems)
        return checkedItems

    def show(self, isMulti):
        show = ''
        Outputlist = self.getCheckItems()
        self.qLineEdit.setReadOnly(False)
        self.selectData=[]
        for i in Outputlist:
            show += i + ';'
            self.selectData.append(i)
        if self.selectedrow_num == 0:
            self.qCheckBox[0].setCheckState(0)
        elif self.selectedrow_num == self.row_num - 1:
            self.qCheckBox[0].setCheckState(2)
        else:
            self.qCheckBox[0].setCheckState(1)

        if show=='': show = self.defaultText
        self.qLineEdit.setText(show)
        self.qLineEdit.setReadOnly(True)

        if self.callable:
            if isMulti:
                if not (len(self.selectData)==0 or len(self.selectData)==(len(self.qCheckBox)-1)): return
            self.callable()

    def All(self, state):
        if state == 2:
            self.isAll = True
            for i in range(1, self.row_num):
                self.qCheckBox[i].setChecked(True)
            self.isAll = False
        elif state == 1:
            if self.selectedrow_num == 0:
                self.qCheckBox[0].setCheckState(2)
        elif state == 0:
            self.isAll = True
            for i in range(self.row_num):
                self.qCheckBox[i].setChecked(False)
            self.isAll = False

    def clear(self):
        self.selectData = []
        for item in self.qCheckBox:
            item.close()
        self.qCheckBox = []
        self.coBox.clear()
        self.qListWidget.close()

    def get(self):
        return self.selectData

    def set(self, items):
        try:
            self.items = items.copy()
            self.items.insert(0, 'all')
            self.row_num = len(self.items)
            self.qListWidget = QListWidget()
            self.selectedrow_num = 0
            self.addQCheckBox(0)
            self.qCheckBox[0].stateChanged.connect(self.All)
            for i in range(1, self.row_num):
                self.addQCheckBox(i)
                self.qCheckBox[i].stateChanged.connect(lambda: self.show(self.isAll))

            self.coBox.setModel(self.qListWidget.model())
            self.coBox.setView(self.qListWidget)

            self.qLineEdit.setText(self.defaultText)
        except Exception as e:
            print(e)